package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.cache.CacheData;
import pers.cj.pdos.cache.CacheRemove;
import pers.cj.pdos.entity.Goods;
import pers.cj.pdos.entity.GoodsUnit;
import pers.cj.pdos.mapper.GoodsUnitMapper;
import pers.cj.pdos.service.IGoodsUnitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.CountGoodsUnitVo;
import pers.cj.pdos.vo.CountVo;
import pers.cj.pdos.vo.GoodsUnitCountVo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class GoodsUnitServiceImpl extends ServiceImpl<GoodsUnitMapper, GoodsUnit> implements IGoodsUnitService {
    @Autowired
    GoodsUnitMapper goodsUnitMapper;

    @Override
    //因为批量价格，只会更新同一商品下的价格，且list不会为空
    @CacheRemove(clazz = GoodsServiceImpl.class, methodName = "getVoById", param = "#list[0].goodsId")
    public void updatePrice(List<GoodsUnit> list) {
        goodsUnitMapper.updatePriceBatch(list);
    }

    @Override
    public boolean checkCodeList(String companyId, List<GoodsUnit> goodsUnitList) {
        return goodsUnitMapper.countByCodeList(companyId,goodsUnitList) > 0;
    }

    @Override
    public Map<String, GoodsUnitCountVo> checkByGoods(List<Goods> goodsList) {
        List<GoodsUnitCountVo> goodsUnitCountVoList =  goodsUnitMapper.checkByGoodsList(goodsList);
        Map<String,GoodsUnitCountVo> map = new HashMap<>();
        for (GoodsUnitCountVo goodsUnitCountVo : goodsUnitCountVoList) {
            if (!goodsUnitCountVo.getId().equals("0") ) {
                map.put(goodsUnitCountVo.getId(), goodsUnitCountVo);
            }
        }
        return map;
    }

    @Override
    @CacheData(clazz = GoodsUnitServiceImpl.class, param = "#companyId")
    public List<GoodsUnit> list(String companyId) {
        return goodsUnitMapper.selectList(new QueryWrapper<GoodsUnit>().eq("company_id",companyId));
    }

    @Override
    public List<CountGoodsUnitVo> checkGoodsUnitListUsed(List<GoodsUnit> list) {
        return goodsUnitMapper.checkGoodsUnitListUsed(list);
    }
}
